import 'package:lunar/lunar.dart';
import 'package:todo_app/controllers/definitions/TodoModel.dart';

extension ExtNum on num? {
  String toW() {
    return this == null ? '0' : ((this! / 100).floor() / 100).toStringAsFixed(2);
  }
}

extension ExtDate on DateTime? {
  bool isToday() {
    return this?.isSameDay(DateTime.now()) == true;
  }

  String toLunarYMD([String defaultValue = '']) {
    return this == null ? defaultValue : Lunar.fromDate(this!).toString();
  }
  // 年-月-日 时:分
  String toYMDHm([String defaultValue = '']) {
    return this == null ? defaultValue : 
    '${this!.year.toString().padLeft(2, '0')}年${this!.month.toString().padLeft(2, '0')}月${this!.day.toString().padLeft(2, '0')}日 ${this!.hour.toString().padLeft(2, '0')}:${this!.minute.toString().padLeft(2, '0')}';
  }

  // 年-月-日
  String toYMD([String defaultValue = '']) {
    return this == null ? defaultValue : '${this!.year.toString().padLeft(2, '0')}年${this!.month.toString().padLeft(2, '0')}月${this!.day.toString().padLeft(2, '0')}日';
  }

  // 年-月
  String toYM() {
    return this == null ? '' : '${this!.year.toString().padLeft(2, '0')}年${this!.month.toString().padLeft(2, '0')}月';
  }

    // 月-日
  String toMD() {
    return this == null ? '' : '${this!.month.toString().padLeft(2, '0')}月${this!.day.toString().padLeft(2, '0')}日';
  }

  // 年-月-日 时:分
  String toHm([String defaultValue = '']) {
    return this == null ? defaultValue : '${this!.hour.toString().padLeft(2, '0')}:${this!.minute.toString().padLeft(2, '0')}';
  }

  // 是否相同日期
  bool isSameDay(DateTime date) {
    return this == null ? false : (this!.year == date.year && this!.month == date.month && this!.day == date.day);
  }

  bool? operator > (DateTime compare) {
    if(this == null) return null;
    return this!.millisecondsSinceEpoch > compare.millisecondsSinceEpoch;
  }

  bool? operator < (DateTime compare) {
    if(this == null) return null;
    return this!.millisecondsSinceEpoch < compare.millisecondsSinceEpoch;
  }
}

extension ExtString on String? {

  String ossPreview() {
    return this == null ? '' : '$this?x-oss-process=image/resize,w1306,h_871';
  }

  String ossVideoPreview() {
    return this == null ? '' : '$this?spm=qipa250&x-oss-process=video/snapshot,t_7000,f_jpg,w_1306,h_871,m_fast';
  }

  DateTime? toDateTime() {
    return DateTime.tryParse(this ?? "")?.toLocal();
  }

  int toInt() {
    return this == null ? 0 : int.tryParse(this!) ?? 0;
  }
}

extension DynamicMap on  Map<String, dynamic> {
  Map<String, dynamic> get noNull {
    Map<String, dynamic> newMap = <String, dynamic>{};
    forEach((key, value) {
      if(value != null) newMap[key] = value;
    });
    return newMap;
  }
}

extension TodoModelExt on TodoModel {
  DateTime? get alertTime {
    if(alert != null) {
      return alert!.next_alert_time.toDateTime() ?? alert!.alert_time.toDateTime();
    }
    return null;
  }
}

extension TodoListExt on List<TodoModel> {
  bool valueInDay(DateTime date) {
    return isNotEmpty && any((todo) => todo.alertTime.isSameDay(date));
  }
}


class PageData<T> {
  int current = 1;
  int pageSize = 10;
  int total = 0;
  List<T> records = [];
  List<T> data = [];
  
  firstPage(List<T> records) {
    this.records = records;
    this.data = records;
  }

  addPage(int current, List<T> records) {
    this.current = current;
    this.data.addAll(records);
    this.records = records;
  }

  // 是否有更多数据
  bool get hasMore {
    return total > pageSize * current;
  }
}

class Pagination {
  int current = 1;
  int pageSize = 10;
  int refreshKey = 0;

  Pagination clone() {
    return Pagination()
      ..current = current
      ..pageSize = pageSize
      ..refreshKey = refreshKey;
  }
}
